<?php
/**
 * Creater: 李青林
 * Date Time : 2018/10/25 10:08
 */

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Models\StatisticsGoodsInfoLog;
use App\Http\Models\UserKillStatus;
use Illuminate\Support\MessageBag;
use Encore\Admin\Widgets\Table;

class LogController extends Controller
{
    use ModelForm;
    protected $headerStr = '日志';
    protected $is_goods = true;
    protected $type = 0;
    protected $search_type = 0;


    public function index()
    {
        $type = request('t');
        $this->search_type = request('type') ? request('type') : 0;
        $this->is_goods = (empty($type) || $type == 'goods') ? true : false;
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->description($this->is_goods ? '商品审核日志' : '纠错审核日志');
            $content->body($this->grid());
        });

    }

    protected function form()
    {
        return Admin::form(StatisticsGoodsInfoLog::class, function (Form $form) {
            $form->display('id', 'ID:');
            $form->text('title', '标题:');
            $form->textarea('content', '内容:');
            $form->tools(function (Form\Tools $tools) {
                $tools->disableBackButton();// 去掉返回按钮
                // $tools->disableListButton(); // 去掉跳转列表按钮
                //$tools->disableDelete(); // 去掉跳转列表按钮
                if (method_exists($tools, 'disableView')) {
                    $tools->disableView();
                }
            });
        });
    }

    protected function grid()
    {
        return Admin::grid(StatisticsGoodsInfoLog::class, function (Grid $grid) {
            if ($this->is_goods) {
                $grid->model()->select(
                    'statistics_goods_info_log.id',
                    'statistics_goods_info_log.gid',
                    'statistics_goods_info_log.type',
                    'statistics_goods_info_log.old_type',
                    'statistics_goods_info_log.change_type',
                    'statistics_goods_info_log.operation_type',
                    'statistics_goods_info_log.online',
                    'statistics_goods_info_log.all_unaudited',
                    'statistics_goods_info_log.today_unaudited',
                    'statistics_goods_info_log.today_adopt',
                    'statistics_goods_info_log.today_refuse',
                    'statistics_goods_info_log.today_adopt',
                    'statistics_goods_info_log.created_at',
                    'goods.id as g_id',
                    'goods.coverimage',
                    'goods.goodstitle',
                    'goods.goodsid',
                    'goods.goodstype',
                    'goods.commissiontype',
                    'goods.commissionrate',
                    'goods.origprice',
                    'goods.voucherprice',
                    'goods.goodslink',
                    'goods_coupon.couponlink',
                    'goods_coupon.couponmoney'
                )->leftjoin('goods', 'goods.id', '=', 'statistics_goods_info_log.gid')
                    ->leftjoin('goods', 'goods.id', '=', 'statistics_goods_info_log.gid')
                    ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                    ->whereIn('statistics_goods_info_log.type', [0, 1, 2])
                    ->orderBy('statistics_goods_info_log.id', 'desc');
                /*    $grid->id('商品Id')->sortable();
                    $grid->goodstitle('商品标题');
                    $grid->old_type('历史状态')->display(function ($value) {
                        $str = show_goods_status_style((int)$value);
                        return $str;
                    });
                    $grid->change_type('当前状态')->display(function ($value) {
                        $str = show_goods_status_style((int)$value);
                        return $str;
                    });
                    $grid->operation_type('操作类型')->display(function ($value) {
                        return $value == 0 ? '接口修改' : ($value == 1 ? '后台修改' : '采集器');
                    });*/

            } else {
                $grid->model()->select(
                    'statistics_goods_info_log.id',
                    'statistics_goods_info_log.gid',
                    'statistics_goods_info_log.type',
                    'statistics_goods_info_log.old_type',
                    'statistics_goods_info_log.change_type',
                    'statistics_goods_info_log.operation_type',
                    'statistics_goods_info_log.online',
                    'statistics_goods_info_log.all_unaudited',
                    'statistics_goods_info_log.today_unaudited',
                    'statistics_goods_info_log.today_adopt',
                    'statistics_goods_info_log.today_refuse',
                    'statistics_goods_info_log.today_adopt',
                    'statistics_goods_info_log.created_at',
                    'goods.coverimage',
                    'goods.id as g_id',
                    'goods.goodstitle',
                    'goods.goodsid',
                    'goods.goodstype',
                    'goods.commissiontype',
                    'goods.commissionrate',
                    'goods.origprice',
                    'goods.voucherprice',
                    'goods.goodslink',
                    'goods_coupon.couponlink',
                    'goods_coupon.couponmoney'
                )
                    ->leftjoin('goods_correction', 'goods_correction.id', '=', 'statistics_goods_info_log.gid')
                    ->leftjoin('goods', 'goods.id', '=', 'goods_correction.gid')
                    ->leftjoin('goods_coupon', 'goods.id', '=', 'goods_coupon.gid')
                    ->where(['statistics_goods_info_log.type' => 3])
                    ->orderBy('statistics_goods_info_log.id', 'desc');
            }
            $grid->id('Id')->sortable();
            $grid->coverimage('商品主图')->display(function ($image) {
                if ($this->change_type == -1) {
                    return '-----';
                }
                $image = json_decode($image, true);
                $image = isset($image['main']) ? all_image_url($image['main']) : '';
                //return '<img src="' . $image . '" width="50" height="50">';
                return '<img src="' . $image . '" width="120" height="120" data-url=1 class="image_show" style="margin-left:5px" onclick=showimage(' . $this->gid . ',"' . $image . '")>';

            });
            $grid->goodstitle('商品信息')->display(function () {
                if ($this->change_type == -1) {
                    return '-----';
                }
                $commissioninfo = commissionrateInfo($this->commissiontype, $this->commissionrate);
                $info = array
                (
                    '商品标题' => '<a target="_blank" href="' . $this->goodslink . '">' . str_limit($this->goodstitle, $limit = 30, $end = '...') . '</a>',
                    '原价/券后价' => '￥' . $this->origprice . ' / ￥' . $this->voucherprice,
                    '优惠券信息' => $this->couponlink ? '<a target="_blank" href="' . $this->couponlink . '">￥ ' . $this->couponmoney . '</a>' : '',
                    '佣金信息' => $commissioninfo,
                    '商品Id' => $this->g_id,
                );
                return new Table([], $info);
            });
            if ($this->is_goods) {
                $grid->old_type('历史状态')->display(function ($value) {
                    return show_goods_status_style((int)$value);
                });
                $grid->change_type('当前状态')->display(function ($value) {
                    return show_goods_status_style((int)$value);
                });
            } else {
                $grid->old_type('历史状态')->display(function ($value) {
                    return show_goods_correction_style($value);
                });
                $grid->change_type('当前状态')->display(function ($value) {
                    return show_goods_correction_style($value);
                });
            }
            $grid->goodstype('商品类型')->display(function ($value) {
                if ($this->change_type == -1) {
                    return $this->type == 0 ? '平推' : ($this->type == 1 ? '爆款' : '秒杀');
                } else {
                    return $value == 0 ? '平推' : ($value == 1 ? '爆款' : '秒杀');
                }

            });
            $grid->operation_type('操作来源')->display(function ($value) {
                return $value == 0 ? '接口操作' : ($value == 1 ? '后台操作' : '采集器操作');
            });
            if ($this->is_goods) {
                $grid->online('在线商品');
                $grid->all_unaudited('全部待审核');
                $grid->today_unaudited('今日待审核');
                $grid->today_adopt('今日通过');
                $grid->today_refuse('今日拒绝');
                $grid->created_at('创建时间');
            } else {
                $grid->all_unaudited('全部待纠错');
                $grid->today_unaudited('今日待纠错');
                $grid->today_adopt('今日纠错');
                $grid->created_at('创建时间');
            }
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();//禁用查询过滤器
                $filter->column(1 / 2, function ($filter) {
                    $filter->where(function ($query) {
                        $query->where('statistics_goods_info_log.created_at', '>', $this->input);
                    }, '更新开始时间')->datetime();
                    $filter->equal('operation_type', '操作来源')->select(['0' => '接口操作', '1' => '后台操作', '2' => '采集器操作']);
                    if ($this->is_goods) {
                        $filter->equal('type', '商品类型')->select(['0' => '平推', '1' => '爆款', '2' => '秒杀']);
                        $filter->equal('change_type', '当前状态')->select([
                            '0' => '未审核',
                            '2' => '审核通过',
                            '3' => '拒绝',
                            '4' => '下架',
                            '6' => '主动下架',
                            '7' => '自动采集删除',
                            '8' => '采集下架',
                            '9' => '机器纠错',
                            '10' => '优惠券过期',
                        ]);
                    }

                });
                $filter->column(1 / 2, function ($filter) {
                    $filter->where(function ($query) {
                        $query->where('statistics_goods_info_log.created_at', '<', $this->input);
                    }, '更新结束时间')->datetime();
                    $filter->equal('goodsid', '淘宝商品ID');
                    $filter->where(function ($query) {
                        $query->whereRaw("`goods`.`id` ={$this->input}");
                    }, '内部商品ID');
                });
            });


            $grid->disableActions();
            $grid->disableExport();
            $grid->disableRowSelector();
            $grid->perPages([10, 20, 30, 40, 50]);
            $grid->paginate(env('Admin_Limit'));
            $grid->disableCreateButton();
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form());
        });
    }

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header($this->headerStr . '管理');
            $content->body($this->form()->edit($id));
        });
    }
}